﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIT.ScrumTool.Service.Interface;
using UIT.ScrumTool.Service.Model;
using UIT.ScrumTool.DTO;

namespace UIT.ScrumTool.Service.Business
{
    public partial class ControlService : ITeamService
    {
        public List<TeamDto> TeamGetByAll()
        {
            List<TeamDto> lsteamDto = new List<TeamDto>();
            try
            {
                List<Team> ls = dataContext.sp_Team_GetByAll().ToList();
                foreach (var team in ls)
                {
                    lsteamDto.Add(ControlService.TeamConvertTeamDto(team));
                }
                return lsteamDto;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public TeamDto TeamGetById(int id)
        {
            try
            {
                return ControlService.TeamConvertTeamDto(dataContext.sp_Team_GetById(id).Single());
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<TeamDto> TeamGetByProjectId(int projectId)
        {
            List<TeamDto> lsteamDto = new List<TeamDto>();
            try
            {
                List<Team> ls = dataContext.sp_Team_GetByProjectId(projectId).ToList();
                foreach (var team in ls)
                {
                    lsteamDto.Add(ControlService.TeamConvertTeamDto(team));
                }
                return lsteamDto;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public int TeamInsert(TeamDto teamDto)
        {
            Team team = new Team();
            team.Name = teamDto.Name;
            team.Description = teamDto.Description;
            team.ProjectId = teamDto.ProjectId;

            try
            {
                dataContext.Teams.InsertOnSubmit(team);
                dataContext.SubmitChanges();
                return team.Id;
            }
            catch (Exception)
            {
                return -1;
            }
        }

        public bool TeamUpdate(TeamDto teamDto)
        {
            try
            {
                dataContext.sp_Team_Update(teamDto.Id, teamDto.Name, teamDto.Description, teamDto.ProjectId);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool TeamDelete(int id)
        {
            try
            {
                dataContext.sp_Team_Delete(id);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static TeamDto TeamConvertTeamDto(Team team)
        {
            TeamDto teamDto = new TeamDto();
            teamDto.Id = team.Id;
            teamDto.Name = team.Name;
            teamDto.Description = team.Description;
            return teamDto;
        }
    }
}
